import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {PIID} from "./PIID";
import {OAD} from "./OAD";
import {COMDCB} from "./COMDCB";
import {Unsigned16} from "./Unsigned16";
import {AxdrOctetString} from "../asn1.axdr/AxdrOctetString";


export class ProxyTransCommandRequest implements AxdrType{

    dataCode : Buffer | null = null;
    piid : PIID | null = null;
    oad : OAD | null = null;
    comdcb : COMDCB | null = null;
    recvIdleSec : Unsigned16 | null = null;
    recvIdleByte : Unsigned16 | null = null;
    transCmd : AxdrOctetString | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    set_all(piid : PIID, oad : OAD, comdcb : COMDCB, recvIdleSec : Unsigned16, recvIdleByte : Unsigned16, transCmd : AxdrOctetString){
        this.piid = piid;
        this.oad = oad;
        this.comdcb = comdcb;
        this.recvIdleSec = recvIdleSec;
        this.recvIdleByte = recvIdleByte;
        this.transCmd = transCmd;
    }


    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);

        this.oad = new OAD();
        codeLength += this.oad.decode(input);

        this.comdcb = new COMDCB();
        codeLength += this.comdcb.decode(input);

        this.recvIdleSec = new Unsigned16();
        this.recvIdleSec.set_const();
        codeLength += this.recvIdleSec .decode(input);

        this.recvIdleByte = new Unsigned16();
        this.recvIdleByte.set_const();
        codeLength += this.recvIdleByte.decode(input);

        this.transCmd = new AxdrOctetString();
        codeLength += this.transCmd.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.transCmd != null && this.recvIdleByte != null && this.recvIdleSec != null
                && this.comdcb != null && this.oad != null && this.piid != null){
                codeLength = 0;
                codeLength += this.transCmd.encode(output);
                codeLength += this.recvIdleByte.encode(output);
                codeLength += this.recvIdleSec.encode(output);
                codeLength += this.comdcb.encode(output);
                codeLength += this.oad.encode(output);
                codeLength += this.piid.encode(output);
            }else{
                throw new Error("transCmd || recvIdleByte || recvIdleSec || comdcb || oad || piid is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.transCmd != null && this.recvIdleByte != null && this.recvIdleSec != null
            && this.comdcb != null && this.oad != null && this.piid != null) {
            return "sequence: {"+ "piid: " + this.piid + ", oad: " + this.oad + ", comdcb: " + this.comdcb + ", recvidlesec: " + this.recvIdleSec + ", recvidlebyte: " + this.recvIdleByte + ", transcmd: " + this.transCmd  + "}";
        } else {
            return "transCmd || recvIdleByte || recvIdleSec || comdcb || oad || piid is null";
        }
    }

}
